package apps.project.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import apps.project.domain.ProjectManageBean;
import apps.project.service.ProjectManageService;

@Controller
@RequestMapping("/project")
public class ProjectController {
	
	@Autowired
	private ProjectManageService projectManageService;
	/**
	 * 获取工作计划的分页信息
	 * @return
	 */
	@RequestMapping("/getPagerList")
	public @ResponseBody Object getWorkList(@RequestParam("type")String type,@RequestParam("page")int page,@RequestParam("size")int size) {
		page=page-1;
		Direction direction=Direction.DESC;
		Order order=new Order(direction,"createTime");
		PageRequest pageReqeust=new PageRequest(page,size,new Sort(order));
		ModelAndView mav = new ModelAndView();
		Page<ProjectManageBean> pageResult=projectManageService.findByTypePager(type, pageReqeust);
        Map attributes = new HashMap();
        attributes.put("total", pageResult.getTotalElements());
        attributes.put("rows", pageResult.getContent());		
		return attributes;
	}
	
	@RequestMapping("/save")
	public @ResponseBody Object saveProjectManager(@ModelAttribute ProjectManageBean proManageBean) {
		System.out.println(proManageBean);
		projectManageService.save(proManageBean);
		return "";
	}
	
	@RequestMapping("/delete")
	public @ResponseBody Object deleteProjectManager(@RequestParam("ids") String[] ids) {
		System.out.println(ids);
		for(String id:ids) {
			ProjectManageBean projectManageBean=projectManageService.getProjectManageDao().findOne(id);
			projectManageService.delete(projectManageBean);
		}
		return null;
	}

	public ProjectManageService getProjectManageService() {
		return projectManageService;
	}

	public void setProjectManageService(ProjectManageService projectManageService) {
		this.projectManageService = projectManageService;
	}
	
	
	

}
